home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / otmvoxel / otmvoxel.cpp < prev    next >
C/C++ Source or Header  |  1994-11-07  |  4KB  |  197 lines

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <math.h>
  4. #include "3dtools.h"
  5. #include "mode13h.h"
  6.  
  7. //  otmvoxel.cpp - released 11-08-94
  8. //  simple voxel landscape demo
  9. //  coded by Voltaire/OTM
  10. //  all source Copyright (C) 1994 Zach Mortensen
  11. //
  12. //  see OTMVOXEL.NFO and OTM-94.NFO for more information
  13. //
  14. //  this source is included purely as an example.  If you want to re-compile
  15. //  it as is, you must link in the following files:
  16. //
  17. //  3dtools.obj
  18. //  mode13h.obj
  19. //  sin.obj
  20. //
  21. //  sources to these files have been previously released in V3DT090.ZIP
  22. //  which is availible via ftp at hornet.eng.ufl.edu in the
  23. //  /demos/code/graph/library directory, as well as at OTM distribution
  24. //  sites listed in OTM-94.NFO
  25.  
  26.  
  27. // z threshold - maximum depth at which a voxel is displayed
  28.  
  29. #define ZTHRESH 2048
  30.  
  31. int *x16;
  32. char *vPage;
  33.  
  34. void initX16(int threshold);
  35. void voxel(int x, int y, int z, int c);
  36.  
  37. void main()
  38. {
  39.     int count, x, y, pos;
  40.     point3d *temp;
  41.  
  42.     // create object and associated points
  43.  
  44.     obj3d *land = new obj3d(0, -75, 1024);
  45.     point3d **point = new point3d* [100];
  46.  
  47.     // set up object as 3d sine curve
  48.  
  49.     for (y = 0; y < 10; y++)
  50.         for (x = 0; x < 10; x++)
  51.             {
  52.                 point[y * 10 + x] = new point3d(x * 10,
  53.                                         (int) ((double) 25 * (sin((double) PI * 18 *x / 180) + sin((double) PI * 18 * y / 180))) + 25,
  54.                                         y * 10);
  55.                 land->addLocalPoint(point[y * 10 + x]);
  56.             }
  57.  
  58.     // initialize speedy size routine and sin/cos tables
  59.  
  60.     initX16(ZTHRESH);
  61.     initSinCos();
  62.  
  63.     // virtual page for off screen drawing
  64.  
  65.     vPage = new char [64000];
  66.  
  67.     // setup video mode etc.
  68.  
  69.     setMode13h(vPage);
  70.  
  71.     // initialize palette
  72.  
  73.     for (count = 1; count < 16; count++)
  74.         set_dac_register(count, 0, 0, 0);
  75.  
  76.     for (count = 1; count < 32; count++)
  77.     {
  78.         set_dac_register(count + 16, 0, count * 2, 0);
  79.         set_dac_register(count + 31 + 16, count * 2, 63, count * 2);
  80.     }
  81.  
  82.     // setup for off screen drawing
  83.  
  84.     setActivePage(pVirtual);
  85.  
  86.     // main loop
  87.  
  88.     while (!kbhit())
  89.     {
  90.         clearScreen(0);
  91.  
  92.         // draw the landscape
  93.  
  94.         for (count = 99; count > 0; count--)
  95.             voxel(point[count]->x2d, point[count]->y2d, point[count]->z3d, point[count]->localY);
  96.  
  97.         // now show the virtual page
  98.  
  99.         flipVPage();
  100.  
  101.         // rotate the landscape 1 degree about the y axis
  102.  
  103.         land->localRotate(0, 1, 0);
  104.  
  105.         // linear sort points according to depth
  106.  
  107.         for (count = 0; count < land->numPoints - 1; count++)
  108.         {
  109.             pos = count;
  110.             for (x = count + 1; x < land->numPoints; x++)
  111.                 if (point[x]->z3d < point[pos]->z3d)
  112.                     pos = x;
  113.  
  114.             if (pos != count)
  115.             {
  116.                 temp = point[pos];
  117.                 point[pos] = point[count];
  118.                 point[count] = temp;
  119.             }
  120.         }
  121.  
  122.     }
  123.  
  124.     // read a keypress
  125.  
  126.     getch();
  127.  
  128.     // back to 80x25 text mode
  129.  
  130.     textMode();
  131.  
  132.     // clean house
  133.  
  134.     delete x16;
  135.     delete vPage;
  136.     delete land;
  137.     delete point;
  138.  
  139. }
  140.  
  141. void initX16(int threshold)
  142. {
  143.     int count;
  144.  
  145.     x16 = new int [threshold];
  146.  
  147.     // setup a table containing perspective sizes of 16 pixels at given
  148.     // depths for speedy reference
  149.  
  150.     for (count = 1; count < threshold; count++)
  151.         x16[count] = 16 * 1024 / count;
  152.  
  153. }
  154.  
  155.  
  156. void voxel(int x, int y, int z, int c)
  157. {
  158.     int index, count, col;
  159.  
  160.     // make sure we're positive
  161.  
  162.     if (c < 0)
  163.         c += 256;
  164.  
  165.     // calculate starting offset into virtual screen
  166.  
  167.     index = (int) vPage + (320 * y) + x;
  168.  
  169.     // display the voxel if it is within the z threshold
  170.  
  171.     if (z < ZTHRESH)
  172.     {
  173.         // draw a shaded square to the offscreen buffer
  174.  
  175.         for (count = 0; count < x16[z]; count++)
  176.         {
  177.             // draw one row of the square
  178.  
  179.             for (col = 0; col < x16[z]; col++)
  180.             {
  181.                 *(char *) index = (char) c;
  182.                 index++;
  183.             }
  184.  
  185.             // go to next line
  186.  
  187.             index += 320;
  188.             index -= x16[z];
  189.  
  190.             // change colors
  191.  
  192.             c--;
  193.         }
  194.     }
  195.  
  196. }
  197.